<?php

namespace backend\models;

use Yii;


/**
 * This is the model class for table "pay_logistics".
 *
 * @property integer $id
 * @property integer $trade_id
 * @property integer $item_id
 * @property string $item_memo
 * @property integer $number
 * @property integer $logistics_state
 * @property string $logistics_trade
 * @property string $logistics_message
 * @property string $logistics_name
 * @property string $logistice_price
 * @property string $send_address
 * @property string $send_photo
 * @property string $accept_address
 * @property string $accept_photo
 * @property integer $admin_uid
 * @property integer $admin_time
 */
class PayLogistics extends \yii\db\ActiveRecord
{
	public $fab_name;
	public $fab_img_url;
	public $mat_name;
	public $mat_img_url;
	public $preview_img_url;
	public $size;
	public $light;
	public $color;
	public $name;
	public $price_base;
	public $price_discount;
	public $price_custom;
	public $_price_discount;
	public $lead_time;
	public $final_price;
	public $sales_service;
	public $user_discount;
	public $operation_address_select;
	public $write_notes;
	public $fab_id;
	public $mat_id;
	public $file;
	public $factory_code;
	public $cost_price;
	public $sum_pay_price;
	public $cost_amount;
	public $send_time_1;
	public $deliver_time_1;
    public $brand_name;
    public $user_name;
    public $address;
    public $trade_state;
    public $price_state;
    public $trade_no;
    public $create_time;
    public $brand_id;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'pay_logistics';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['trade_id', 'item_id', 'number', 'logistics_state', 'admin_uid', 'admin_time','audit_pay','apply_pay_time','audit_pay_time','actual_pay_time','doing'], 'integer'],
            [['apply_pay_uid','audit_pay_uid','actual_pay_uid','operation_uid','send_time','deliver_time'],'integer'],
            [['item_memo', 'logistics_message', 'send_address', 'accept_address','operation_address','pay_done_list','notes','write_notes','actual_pay_file'], 'string'],
            [['logistice_price','ask_cost_price','ask_cost_price_0','apply_pay','payed','cost_price','sum_pay_price','cost_amount','user_discount'], 'number'],
            [['logistics_trade','actual_pay','logistics_no','send_time_1','deliver_time_1'], 'string', 'max' => 128],
            [['logistics_name'], 'string', 'max' => 64],
            [['send_photo', 'accept_photo','operation_photo'], 'string', 'max' => 18],
            ['operation_address_select','safe'],
            [['file'], 'file','extensions' => 'txt,doc,xls,xlsx,pdf,png,jpg,jpeg,gif,bmp','maxSize'=>'1024*1024*2',
            'tooBig'=>'文件不要超过2M'],
            [['factory_code'],'string','max'=>50],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'trade_id' => Yii::t('app', 'Trade ID'),
            'item_id' => Yii::t('app', 'Item ID'),
            'item_memo' => Yii::t('app', 'Item Memo'),
            'number' => Yii::t('app', 'Number'),
            'logistics_state' => Yii::t('app', 'Logistics State'),
            'logistics_trade' => Yii::t('app', 'Logistics Trade'),
            'logistics_message' => Yii::t('app', 'Logistics Message'),
            'logistics_name' => Yii::t('app', 'Logistics Name'),
            'logistice_price' => Yii::t('app', 'Logistice Price'),
            'send_address' => Yii::t('app', 'Send Address'),
            'send_photo' => Yii::t('app', 'Send Photo'),
            'accept_address' => Yii::t('app', 'Accept Address'),
            'accept_photo' => Yii::t('app', 'Accept Photo'),
            'admin_uid' => Yii::t('app', 'Admin Uid'),
            'admin_time' => Yii::t('app', 'Admin Time'),
            'operation_address' => Yii::t('app', 'Operation Address'),
            'operation_photo' => Yii::t('app', 'Operation Photo'),
            'ask_cost_price' => Yii::t('app', 'Logistics Ask Cost Price'),
            'ask_cost_price_0' => Yii::t('app', 'Logistics Ask Cost Price Change'),
            'apply_pay' => Yii::t('app', 'Logistics Apply Pay'),
            'apply_pay_time' => Yii::t('app', 'Logistics Apply Pay Time'),
            'apply_pay_uid' => Yii::t('app', 'Logistics Apply Pay Uid'),
            'audit_pay' => Yii::t('app', 'Logistics Audit Pay'),
            'audit_pay_time' => Yii::t('app', 'Logistics Audit Pay Time'),
            'audit_pay_uid' => Yii::t('app', 'Logistics Audit Pay Uid'),
            'actual_pay' => Yii::t('app', 'Logistics Actual Pay'),
            'actual_pay_time' => Yii::t('app', 'Logistics Actual Pay Time'),
            'actual_pay_uid' => Yii::t('app', 'Logistics Actual Pay Uid'),
            'doing' => Yii::t('app', 'Logistics Pay Doing'),
            'pay_done_list' => Yii::t('app', 'Logistics Pay Done List'),
            'notes'=>Yii::t('app', 'Logistics Notes'),
            'write_notes'=>Yii::t('app', 'Logistics Notes'),
            'logistics_no'=>Yii::t('app', 'Logistics No'),
            'file'=>Yii::t('app', 'Logistics Finance File'),
            'payed'=>Yii::t('app', 'Logistics Finance Payed'),
            'send_time'=>Yii::t('app', 'Logistics Send Time'),
            'send_time_1'=>Yii::t('app', 'Logistics Send Time'),
            'deliver_time'=>Yii::t('app', 'Logistics Deliver Time'),
            'deliver_time_1'=>Yii::t('app', 'Logistics Deliver Time'),
            'logistics_company_name'=>Yii::t('app', 'Logistics Company Name'),
            'brand_name'=>Yii::t('app', 'Brand Name'),
            'user_name'=>Yii::t('app', 'Username'),
            'trade_no'=>"订单号"
        ];
    }
    public function getTradeinfo()
    {
    	return $this->hasOne(PayTrade::className(),['id'=>'trade_id']);
    }
    public function getIteminfo()
    {
    	return $this->hasOne(SysItem::className(),['id'=>'item_id']);
    }
    public function getLogisticsCompanies()
    {
        return $this->hasOne(GrpKuaidi::className(), ['code'=>'logistics_name']);
    }
    /*计合进价*/
    static function  sum_cost_price(){
    	$model = PayLogistics::find()
    	->joinWith(['iteminfo'])
    	->where("pay_trade.trade_class=1 and pay_logistics.ask_cost_price =0")
    	->groupBy(['sys_item.brand_id','trade_id'])
    	->all();
    	 
    	if(!empty($model)){
    		
    		foreach ($model as $key => $val){
    	   
    			$trade_id = $val->trade_id;
    			$brand_id = $val->iteminfo->brand_id;
    			$item_model  = PayLogistics::find()->joinWith(['iteminfo'])
    			->where("trade_id={$trade_id} and sys_item.brand_id={$brand_id}")->all();
    			$ask_cost_price = 0.00;//进价
    			$logistics_ids = array();//物流ids，用于更新进价
    			foreach ($item_model as $k => $v){
    				$item_meno = json_decode($v['item_memo'],true);
    				$price_id = isset($item_meno['price_id']) ? intval($item_meno['price_id']):0;
    				$item_id = isset($item_meno['item_id']) ? intval($item_meno['item_id']):0;
    
    				$item = SysItem::findOne($item_id);
    				$price_base = !empty($item) ? $item['price_cost'] : 0.00;
    				$price = SysItemPrices::findOne($price_id);
    				$price_custom = !empty($price) ? $price['price_cost'] : 0.00;
    					
    				$one_cost_price = $price_base + $price_custom;
    				$ask_cost_price += $one_cost_price;
    				//物流id
    				array_push($logistics_ids, $v->id);
    			}
    			$where = implode(',', $logistics_ids);
    	   
    			PayLogistics::updateAll(['ask_cost_price'=>$ask_cost_price],"id in ($where)");
    		}
    	}
    }
}
